/**
 * 
 */
package com.huarui.notice;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author sunpeng
 * 
 */
public class NoticeDealerFactoryImpl implements ApplicationContextAware,
		NoticeDealerFactory {

	private Map<String, NoticeDealer> noticeDealerMap;

	@SuppressWarnings("unchecked")
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		Map<String, NoticeDealer> noticeDealers = (Map<String, NoticeDealer>) applicationContext
				.getBeansOfType(NoticeDealer.class);
		if (noticeDealers != null) {
			this.noticeDealerMap = new HashMap<String, NoticeDealer>();
			for (NoticeDealer noticeDealer : noticeDealers.values()) {
				this.noticeDealerMap.put(noticeDealer.aboutJsModuleId(),
						noticeDealer);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huarui.notice.NoticeDealerFactory#getNoticeDealer(java.lang.String)
	 */
	public NoticeDealer getNoticeDealer(String jsModuleId) {
		if (StringUtils.isEmpty(jsModuleId)) {
			return new NoticeDealer() {
				@Override
				public boolean deal(Notice notice, boolean dealed,
						Map<String, String[]> params,
						Map<String, Object> resultHolder) {
					return true;
				}

				@Override
				public String aboutJsModuleId() {
					return null;
				}
			};
		}
		return this.noticeDealerMap == null?null:this.noticeDealerMap.get(jsModuleId);
	}
}
